package com.wgj.android.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jiaai.xui.XUI;
import com.jiaai.xui.utils.XDateUtils;
import com.jiaai.xui.utils.XDensityUtils;
import com.wgj.android.R;
import com.wgj.android.utils.TextViweUtils;
import com.wgj.android.utils.fonat.IconFonat;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/12.
 */

public class ManyDiadlog extends DialogFragment {
    private  Dialog dialog;
    private View view;

    private  ManyDiadlogListener diadlogListener;

    @Nullable
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if(dialog==null){
            // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
            initDialogStyle(view);
        }
        return dialog;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(view==null){
            super.onCreate(savedInstanceState);
            view = LayoutInflater.from(getActivity()).inflate(R.layout.many_dilaog_view, null, false);

        }
        ButterKnife.bind(this,view);


    }

    @OnClick({R.id.item1,R.id.item2,R.id.item3})void  onItemClick(View view){
        switch (view.getId()){
            case R.id.item1:
                 if(diadlogListener!=null){
                     diadlogListener.onItemClick(0);
                 }
                break;
            case R.id.item2:
                if(diadlogListener!=null){
                    diadlogListener.onItemClick(1);
                }
                break;
            case R.id.item3:
                if(diadlogListener!=null){
                    diadlogListener.onItemClick(2);
                }
                break;
        }
    }

    public ManyDiadlog setDiadlogListener(ManyDiadlogListener diadlogListener) {
        this.diadlogListener = diadlogListener;
        return this;
    }

    private void initDialogStyle(View view) {
        dialog = new Dialog(getActivity(), R.style.dialog_setting);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.popupwindow_anim_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = XDensityUtils.getScreenWidth()-XDensityUtils.dp2px(20);
        window.setAttributes(lp);
    }

    public  interface  ManyDiadlogListener{
        void  onItemClick(int index);
    }
}
